package com.dotstone.chipism.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.dotstone.chipism.bean.Device;
import com.dotstone.chipism.bean.DeviceType;
import com.dotstone.chipism.bean.Home;
import com.dotstone.chipism.bean.MySocket;
import com.dotstone.chipism.bean.Order;
import com.dotstone.chipism.bean.Room;
import com.dotstone.chipism.bean.Scene;
import com.dotstone.chipism.bean.Scene_Task;
import com.dotstone.chipism.bean.ShareInfo;

import android.content.Context;
import android.util.Log;

/*************************************************************************
 * 公 司： 深圳市高金科技有限公司 作 者： 深圳市高金科技有限公司 文件名称：DeviceManager.java 内容摘要：设备管理类
 * 通过该单例管理（增删改查）设备的数据 方便各Activity调用 日 期： 2016-11-28
 ************************************************************************/
public class DeviceManager {

    private List<Device> mAllDeviceList = new ArrayList<Device>();// 本地所有设备的集合
    private List<Device> mRFDevices = new ArrayList<Device>();
    private List<Device> mNoWlanDevices = new ArrayList<Device>();
    private List<Device> mWlans = new ArrayList<Device>();
    private List<Device> mSwitchs = new ArrayList<Device>();
    private List<Device> mSockets = new ArrayList<Device>();
    private List<Device> mAirs = new ArrayList<Device>();
    private List<Device> mAudios = new ArrayList<Device>();
    private List<Device> mLights = new ArrayList<Device>();
    private List<Device> mLocks = new ArrayList<Device>();
    private List<Device> mCameras = new ArrayList<Device>();
    private List<Device> mCurtains = new ArrayList<Device>();
    private List<Device> mWireless = new ArrayList<Device>();
    private List<Device> mLinghuo = new ArrayList<Device>();
    private List<Device> mDanhuo = new ArrayList<Device>();
    private List<Device> mChazuo = new ArrayList<Device>();

    private List<Home> mHomes = new ArrayList<Home>();
    private List<Scene> mScenes = new ArrayList<Scene>();
    private List<Scene_Task> mTasks = new ArrayList<Scene_Task>();
    private List<Order> orders = new ArrayList<Order>();
    private List<Device> mSaveDevice = new ArrayList<Device>(); // 缓存尚未保存的设备
    private HashMap<String, Timer> mTimes = new HashMap<String, Timer>();
    public static int mDeviceCounts;
    private boolean isExists;// 判断设备是否已经存在本地
    public static String hid = "";
    public List<Room> mRooms = new ArrayList<Room>();
    public static DeviceManager mDeviceManager = null;
    private Order currentOrder = new Order();
    private Context mContext;
    private String mDeviceName;
    private String mDeviceAddress;
    private String mJsonStr;
    private String mWlanName;
    private String mWlanBssid;
    private String mWlanId;
    private String mDeviceId;
    public String homeName;
    private int type;
    private int mSDKVersion;
    private MySocket socket;
    private int DEVICE_DENSITY_DPI;
    private HashMap<String, Boolean> powerMap = new HashMap<String, Boolean>();
    private HashMap<String, Boolean> roomStatusMap = new HashMap<String, Boolean>();
    private HashMap<String, Boolean> sceneStatusMap = new HashMap<String, Boolean>();
    private HashMap<String, Scene> scenesMap = new HashMap<String, Scene>();
    private HashMap<String, Room> roomsMap = new HashMap<String, Room>();
    private HashMap<String, Device> devicesMap = new HashMap<String, Device>();
    private HashMap<String, List<Device>> sceneChildDevicesMap = new HashMap<String, List<Device>>();
    private HashMap<String, List<Device>> groupChildDevicesMap = new HashMap<String, List<Device>>();
    private Device mSaveWlan = new Device();
    private boolean needToCheckTemp = false;
    private boolean needToRefreshScenes = true;

    public boolean isNeedToCheckTemp() {
        return needToCheckTemp;
    }

    public void setNeedToCheckTemp(boolean needToCheckTemp) {
        this.needToCheckTemp = needToCheckTemp;
    }

    public int getmSDKVersion() {
        return mSDKVersion;
    }

    public String getmWlanId() {
        return mWlanId;
    }

    public int getDEVICE_DENSITY_DPI() {
        return DEVICE_DENSITY_DPI;
    }

    public void setDEVICE_DENSITY_DPI(int dEVICE_DENSITY_DPI) {
        DEVICE_DENSITY_DPI = dEVICE_DENSITY_DPI;
    }

    public void setmWlanId(String mWlanId) {
        this.mWlanId = mWlanId;
    }

    public void setmSDKVersion(int mSDKVersion) {
        this.mSDKVersion = mSDKVersion;
    }

    public static DeviceManager getInstance() {
        if (mDeviceManager == null) {
            mDeviceManager = new DeviceManager();// 单例对象实例
        }
        return mDeviceManager;
    }

    private DeviceManager() {

    }

    private Object readResolve() {
        return getInstance();
    }

    public List<Device> getmSaveDevice() {
        return mSaveDevice;
    }

    public void setmSaveDeviceList(List<Device> mSaveDevice) {
        this.mSaveDevice = mSaveDevice;
    }

    public boolean isNeedToRefreshScenes() {
        return needToRefreshScenes;
    }

    public void setNeedToRefreshScenes(boolean needToRefreshScenes) {
        this.needToRefreshScenes = needToRefreshScenes;
    }

    /**
     * 方法：clear 用途：清空各类型设备的集合
     */
    public void clearDevice() {
        mAirs.clear();
        mAudios.clear();
        mSockets.clear();
        mWlans.clear();
        mSwitchs.clear();
        mLights.clear();
        mLocks.clear();
        mAllDeviceList.clear();
        mNoWlanDevices.clear();
        mRFDevices.clear();
        mCameras.clear();
    }

    public void clearScene() {
        mScenes.clear();
        mTasks.clear();
    }

    public String getDeviceAddress() {
        return mDeviceAddress;
    }

    public void setDeviceAddress(String address) {
        this.mDeviceAddress = address;
    }

    public int getDeviceType() {
        return type;
    }

    public void setDeviceType(int type) {
        this.type = type;
    }

    public String getDeviceNmae() {
        return mDeviceName;
    }

    public void setDeviceNmae(String name) {
        this.mDeviceName = name;
    }

    public Context getContext() {
        return mContext;
    }

    public void clearRoom() {
        mRooms.clear();
    }

    public List<Room> getRooms() {
        return mRooms;
    }

    public void addRoom(Room room) {
        boolean e = false;
        for (int i = 0; i < mRooms.size(); i++) {
            if (mRooms.get(i).getRoomId().equals(room.getRoomId())) {
                e = true;
            }
        }
        if (!e) {
            room.setStatus(1);
            mRooms.add(room);
            if (roomsMap.get(room.getRoomId()) == null) {
                // Log.i("wmy", "加入roomsmap的Room的cmds"+room.getCmds().size());
                roomsMap.put(room.getRoomId(), room);
            }
            if (roomStatusMap.get(room.getRoomId()) == null) {
                roomStatusMap.put(room.getRoomId(), false);
            }
        }
    }

    public void putRoomInMap(Room room) {
        // Log.i("wmy", "重新加入roomsmap的Room的cmds"+room.getCmds().size());
        roomsMap.put(room.getRoomId(), room);
    }

    public void addHome(Home home) {
        boolean e = false;
        for (int i = 0; i < mHomes.size(); i++) {
            if (mHomes.get(i).getHid().equals(home.getHid())) {
                e = true;
            }
        }
        if (!e) {
            mHomes.add(home);
        }
    }

    public void clearHouse() {
        mHomes.clear();
    }

    public void addScene(Scene scene) {
        boolean e = false;
        for (int i = 0; i < mScenes.size(); i++) {
            if (mScenes.get(i).getSceneId().equals(scene.getSceneId())) {
                e = true;
            }
        }
        if (!e) {
            mScenes.add(scene);
            if (scenesMap.get(scene.getSceneId()) == null) {
                scenesMap.put(scene.getSceneId(), scene);
            }
            if (sceneStatusMap.get(scene.getSceneId()) == null) {
                sceneStatusMap.put(scene.getSceneId(), false);
            }
        }
    }

    public boolean checkSceneable(Scene s) {
        List<Device> devices = s.getDevices();
        List<String> wlans = new ArrayList<String>();
        if (devices.size() == 0) {
            return false;
        } else {
            for (int i = 0; i < devices.size(); i++) {
                String mid = devices.get(i).getMainDeviceId();
                wlans.add(mid);
            }
            boolean useable = true;
            for (int i = 0; i < wlans.size(); i++) {
                if (SocketManager.getInstance().mStatusMap.get(wlans.get(i)) != 1) {
                    useable = false;
                }
            }
            return useable;
        }

    }

    public List<Home> getHome() {
        List<Home> homes = new ArrayList<Home>();
        for (int i = 0; i < mHomes.size(); i++) {
            if (mHomes.get(i).getStatus() == 9) {
                homes.add(mHomes.get(i));
            }
        }
        return homes;
    }

    public List<Home> getAllHome() {
        return mHomes;
    }

    public void addTask(Scene_Task task) {
        boolean e = false;
        for (int i = 0; i < mTasks.size(); i++) {
            if (mTasks.get(i).getStId().equals(task.getStId())) {
                e = true;
                break;
            }
        }
        if (!e) {
            // for (int i = 0; i < mAllDeviceList.size(); i++) {
            // if (task.getDeviceId().equals(
            // mAllDeviceList.get(i).getDeviceID())) {
            // task.setDeviceName(mAllDeviceList.get(i).getDeviceName());
            // }
            // }
            if (getDeviceById(task.getDeviceId()) != null) {
                task.setDeviceName(getDeviceById(task.getDeviceId()).getDeviceName());
                mTasks.add(task);
            }
        }
    }

    public void clearTask() {
        mTasks.clear();
    }

    public List<Scene> getScenes() {
        return mScenes;
    }

    public void deleteRoom(int position) {
        mRooms.remove(position);
    }

    /**
     * 方法：getAllDevice 用途：将各类型的设备集合中的元素取出，添加至总集合中，返回给调用者 用以加载至主界面的设备列表中
     */
    public List<Device> getAllDevice() {
        return mAllDeviceList;
    }

    public List<Device> getNoWlansDevice() {
        mNoWlanDevices.clear();
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (mAllDeviceList.get(i).getDeviceType() != DeviceType.DEVICE_TYPE_WLAN) {
                mNoWlanDevices.add(mAllDeviceList.get(i));
            }
        }
        return mNoWlanDevices;
    }

    public List<Device> getMessionDevice() {
        mRFDevices.clear();
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_RGB_LIGHT
                    || mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_SOCKET
                    || mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_SWITCH) {
                mRFDevices.add(mAllDeviceList.get(i));
            }
        }
        return mRFDevices;
    }

    public List<Device> getRFDevice() {
        mRFDevices.clear();
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_RGB_LIGHT
                    || mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_SOCKET
                    || mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_SWITCH
                    || mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_LOCK) {
                mRFDevices.add(mAllDeviceList.get(i));
            }
        }
        return mRFDevices;
    }

    public List<Device> getNoLockDevice() {
        mRFDevices.clear();
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (mAllDeviceList.get(i).getDeviceType() != DeviceType.DEVICE_TYPE_LOCK
                    && mAllDeviceList.get(i).getDeviceType() != DeviceType.DEVICE_TYPE_WLAN) {
                mRFDevices.add(mAllDeviceList.get(i));
            }
        }
        return mRFDevices;
    }

    public List<Device> getWlans() {
        return mWlans;
    }

    public List<Device> getSwitchs() {
        mSwitchs.clear();
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_SWITCH
                    ||   mAllDeviceList.get(i).getDeviceType() == DeviceType.LINGHUO_SWITCH_1 || mAllDeviceList.get(i).getDeviceType() == DeviceType.LINGHUO_SWITCH_2 || mAllDeviceList.get(i).getDeviceType() == DeviceType.LINGHUO_SWITCH_3
                    || mAllDeviceList.get(i).getDeviceType() == DeviceType.DANHUO_SWITCH_1 || mAllDeviceList.get(i).getDeviceType() == DeviceType.DANHUO_SWITCH_2 || mAllDeviceList.get(i).getDeviceType() == DeviceType.DANHUO_SWITCH_3) {
                mSwitchs.add(mAllDeviceList.get(i));
            }
        }
        return mSwitchs;
    }

    public List<Device> getSockets() {
        mSockets.clear();
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_SOCKET || mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_CHAZUO) {
                mSockets.add(mAllDeviceList.get(i));
            }
        }
        return mSockets;
    }

    public List<Device> getCurtains() {
        mCurtains.clear();
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_CURTAIN) {
                mCurtains.add(mAllDeviceList.get(i));
            }
        }
        return mCurtains;
    }

    public List<Device> getWireless() {
        mWireless.clear();
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (mAllDeviceList.get(i).getDeviceType() == DeviceType.WIRELSEE_SWITCH_1 || mAllDeviceList.get(i).getDeviceType() == DeviceType.WIRELSEE_SWITCH_2 || mAllDeviceList.get(i).getDeviceType() == DeviceType.WIRELSEE_SWITCH_3) {
                mWireless.add(mAllDeviceList.get(i));
            }
        }
        return mWireless;
    }

    public List<Device> getLinghuo() {
        mLinghuo.clear();
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (mAllDeviceList.get(i).getDeviceType() == DeviceType.LINGHUO_SWITCH_1 || mAllDeviceList.get(i).getDeviceType() == DeviceType.LINGHUO_SWITCH_2 || mAllDeviceList.get(i).getDeviceType() == DeviceType.LINGHUO_SWITCH_3) {
                mLinghuo.add(mAllDeviceList.get(i));
            }
        }
        return mLinghuo;
    }

    public List<Device> getChazuo() {
        mChazuo.clear();
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_CHAZUO) {
                mChazuo.add(mAllDeviceList.get(i));
            }
        }
        return mChazuo;
    }


    public List<Device> getDanhuo() {
        mDanhuo.clear();
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (mAllDeviceList.get(i).getDeviceType() == DeviceType.DANHUO_SWITCH_1 || mAllDeviceList.get(i).getDeviceType() == DeviceType.DANHUO_SWITCH_2 || mAllDeviceList.get(i).getDeviceType() == DeviceType.DANHUO_SWITCH_3) {
                mDanhuo.add(mAllDeviceList.get(i));
            }
        }
        return mDanhuo;
    }

    public List<Device> getCameras() {
        mCameras.clear();
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_SECURITY_CAMERA) {
                mCameras.add(mAllDeviceList.get(i));
            }
        }
        return mCameras;
    }

    public List<Device> getRemotes() {
        mAirs.clear();
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_AIR_CONTROLLER
                    || mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_BOX2
                    || mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_DVD
                    || mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_FAN
                    || mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_TV
                    || mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_PROJECTOR) {
//				Log.i("wmy", "遥控器设备 "+mAllDeviceList.get(i).getDeviceName());
                mAirs.add(mAllDeviceList.get(i));
            }
        }
        return mAirs;
    }

    public List<Device> getLights() {
        mLights.clear();
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_RGB_LIGHT) {
                mLights.add(mAllDeviceList.get(i));
            }
        }
        return mLights;
    }

    public List<Device> getAudios() {
        mAudios.clear();
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_AUDIO) {
                mAudios.add(mAllDeviceList.get(i));
            }
        }
        return mAudios;
    }

    public List<Device> getLocks() {
        mLocks.clear();
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (mAllDeviceList.get(i).getDeviceType() == DeviceType.DEVICE_TYPE_LOCK) {
                mLocks.add(mAllDeviceList.get(i));
            }
        }
        return mLocks;
    }

    public List<Scene_Task> getTasks() {
        return mTasks;
    }

    public List<Scene_Task> getTasksById(String sceneId) {
        List<Scene_Task> tasks = new ArrayList<Scene_Task>();
        for (int i = 0; i < mTasks.size(); i++) {
            if (mTasks.get(i).getSceneId().equals(sceneId)) {
                tasks.add(mTasks.get(i));
            }
        }
        return tasks;
    }

    public String getHomeName() {
        return homeName;
    }

    /**
     * 方法：deleteDevice 用途：根据设备在集合中的位置删除设备
     */
    public void deleteDevice(int position) {
        mAllDeviceList.remove(position);
    }

    public void deleteDevice(Device device) {
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (mAllDeviceList.get(i).getDeviceID().equals(device.getDeviceID())) {
                mAllDeviceList.remove(i);
                break;
            }
        }
    }

    public void upDateDeviceName(Device device) {

        devicesMap.put(device.getDeviceID(), device);
    }

    // public void deleteWlan(String deviceId){
    // for(int i = 0;i<mWlans.size();i++){
    //
    // }
    // }

    public void addOrder(Order order) {
        orders.add(order);
    }

    public void clearOrder() {
        orders.clear();
    }

    /**
     * 方法：addMore 用途：添加设备，先做是否存在的判断，如不存在才添加
     */
    public void addDevice(Device device) {
        Log.i("wmy", "添加设备" + device.getDeviceName() + " " + device.getDeviceID());
        isExists = false;
        for (int i = 0; i < mAllDeviceList.size(); i++) {
//			Log.e("wmy", " 判断设备是否存在 " +mAllDeviceList.get(i).getDeviceID()+" "+mAllDeviceList.get(i).getDeviceName()+" "+device.getDeviceID()+" "+device.getDeviceName());
            if (mAllDeviceList.get(i).getDeviceID().equals(device.getDeviceID())) {
                isExists = true;
            }
        }
        if (!isExists) {

            mAllDeviceList.add(device);
            if (devicesMap.get(device.getDeviceID()) == null) {
                devicesMap.put(device.getDeviceID(), device);
            }
            if (powerMap.get(device.getDeviceID()) == null) {
                powerMap.put(device.getDeviceID(), false);
            }
            if (device.getDeviceType() == DeviceType.DEVICE_TYPE_WLAN) {
                mWlans.add(device);
            } else {
                mNoWlanDevices.add(device);
            }
        } else {
//			Log.i("wmy", "不存入该设备"+device.getDeviceName());
        }

    }

    /**
     * 方法：resetDeviceName 用途：重设网关名称
     */
    public void resetDeviceName(int position, String name) {
        mAllDeviceList.get(position).setDeviceName(name);
    }

    /**
     * 返回true为已存在
     */
    public boolean checkName(String name) {
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (name.equals(mAllDeviceList.get(i).getDeviceName())) {
                return true;
            }
        }
        return false;
    }

    public boolean checkRoomExist(String name) {
        List<Room> mRooms = getRooms();
        for (int i = 0; i < mRooms.size(); i++) {
            if (name.equals(mRooms.get(i).getRoomName())) {
                return true;
            }
        }
        return false;
    }

    public boolean checkSceneExist(String name) {
        for (int i = 0; i < mScenes.size(); i++) {
            if (name.equals(mScenes.get(i).getSceneName())) {
                return true;
            }
        }
        return false;
    }

    public Device checkExisted(String strAddress) {
        List<Device> devices = getAllDevice();
        for (Device d : devices) {
            if (strAddress.equals(d.getDeviceAddress())) {
                return d;
            }
        }
        return null;
    }

    public boolean checkExist(String strAddress) {
        List<Device> devices = getNoWlansDevice();
        for (Device d : devices) {
            if (d.getMainDeviceId() != null) {
                if (d.getMainDeviceId().equals(mWlanId) && (strAddress.equals(d.getDeviceAddress()))) {
                    return true;
                }
            } else {
                // return true;
            }
        }
        return false;
    }

    public String getmJsonStr() {
        return mJsonStr;
    }

    public void setmJsonStr(String mJsonStr) {
        this.mJsonStr = mJsonStr;
    }

    public String getmWlanName() {
        return mWlanName;
    }

    public void setmWlanName(String mWlanName) {
        this.mWlanName = mWlanName;
    }

    public List<Order> getOrders() {
        return orders;
    }

    public void setOrders(List<Order> orders) {
        this.orders = orders;
    }

    public String getmWlanBssid() {
        return mWlanBssid;
    }

    public void setmWlanBssid(String mWlanBssid) {
        this.mWlanBssid = mWlanBssid;
    }

    public String getmDeviceId() {
        return mDeviceId;
    }

    public void setmDeviceId(String mDeviceId) {
        this.mDeviceId = mDeviceId;
    }

    public void startScene(String sceneId) {
        Timer timer = new Timer();
        mTimes.put(sceneId, timer);
        final List<Scene_Task> tasks = new ArrayList<Scene_Task>();
        for (int i = 0; i < mTasks.size(); i++) {
            if (mTasks.get(i).getSceneId().equals(sceneId)) {
                tasks.add(mTasks.get(i));
            }
        }
        for (int i = 0; i < tasks.size(); i++) {

            final int position = i;
            TimerTask timerTask = new TimerTask() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    String mainDeviceId = "";
                    for (Device device : mAllDeviceList) {
                        if (device.getDeviceID().equals(tasks.get(position).getDeviceId())) {
                            mainDeviceId = device.getMainDeviceId();
                            break;
                        }
                    }
                    socket = SocketManager.getInstance().sMap.get(mainDeviceId);
                    if (socket != null) {
                        if (socket.isLocalSuccess()) {
                            // socket.sendMqttData(deviceses.get(i).getCloseCmd());
                            // Log.i("wmy", "发送指令" + System.currentTimeMillis()
                            // + " " + tasks.get(position).getSceneCmd());
                            socket.sendMqttDataDelay(tasks.get(position).getSceneCmd());
                        } else {
                            SocketManager.getInstance().sendRemoteData(mainDeviceId, tasks.get(position).getSceneCmd());
                        }
                    } else {
                    }
                }
            };

            timer.schedule(timerTask, tasks.get(position).getDelayed() * 1000);
        }
    }

    public void addSaveDevice(Device device) {
        isExists = false;
        for (int i = 0; i < mSaveDevice.size(); i++) {
            if (mSaveDevice.get(i).getDeviceAddress().equals(device.getDeviceAddress())) {
                isExists = true;
            }
        }
        if (!isExists) {
            // Log.i("wmy", "添加设备 = " + device.getDeviceAddress());
            mSaveDevice.add(device);
        }
    }

    public void clearSaveDevice() {
        mSaveDevice.clear();
    }

    public Device getmSaveWlan() {
        return mSaveWlan;
    }

    public void setmSaveWlan(Device mSaveWlan) {
        this.mSaveWlan = mSaveWlan;
    }

    public void stopScene(String sceneId) {
        if (mTimes.get(sceneId) != null) {
            // Log.i("wmy", "暂停");
            mTimes.get(sceneId).cancel();
        }
    }

    public Order getCurrentOrder() {
        return currentOrder;
    }

    public void setCurrentOrder(Order currentOrder) {
        this.currentOrder = currentOrder;
    }

    public void setChildDevice() {
        for (Device wlan : mWlans) {
            List<Device> devices = new ArrayList<Device>();
            for (int i = 0; i < mNoWlanDevices.size(); i++) {
                if (wlan.getDeviceID().equals(mNoWlanDevices.get(i).getMainDeviceId())) {
                    boolean e = false;
                    for (Device d : devices) {
                        if (d.getDeviceID().equals(mNoWlanDevices.get(i).getDeviceID())) {
                            e = true;
                        }
                    }
                    if (!e) {
                        devices.add(mNoWlanDevices.get(i));
                    }
                }
            }
            wlan.setChildDevices(devices);
        }
    }

    public void setDeviceState(String id, boolean power) {
        powerMap.put(id, power);
    }

    public boolean checkDeviceState(String id) {
        if (powerMap.get(id) != null) {
            return powerMap.get(id);
        } else {
            return false;
        }
    }

    public void setRoomState(String id, boolean power) {
        roomStatusMap.put(id, power);
    }

    public boolean checkRoomState(String id) {
        if (roomStatusMap.get(id) != null) {
            return roomStatusMap.get(id);
        } else {
            return false;
        }
    }

    public void setSceneState(String id, boolean power) {
        sceneStatusMap.put(id, power);
    }

    public boolean checkSceneState(String id) {
        if (sceneStatusMap.get(id) != null) {
            return sceneStatusMap.get(id);
        } else {
            return false;
        }
    }

    public List<Scene> getShareableScenes() {
        List<Scene> scenes = new ArrayList<Scene>();
        Log.i("wmy", "添加分享" + 770 + " " + ShareManager.getInstance().getShareType());
        List<ShareInfo> shares = ShareManager.getInstance().getAllShares();
        switch (ShareManager.getInstance().getShareType()) {
            case 1:
                // 获取自己拥有的全部情景
                // 获取自己拥有的全部分享
                // 遍历分享 如果是自己创建的 remove掉 保留别人分享给自己的
                // 遍历别人分享给自己的分享 获取所有的sceneId
                // 遍历sceneId,与自己有的情景对比
                // 如果有 从
                scenes = ShareManager.getInstance().getFamilyScenes();
                break;
            case 2:
                scenes = ShareManager.getInstance().getFriendScenes();
                break;

            default:
                break;
        }
        return scenes;
    }

    public List<Device> getShareableDevices() {
        List<Device> devices = new ArrayList<Device>();
        switch (ShareManager.getInstance().getShareType()) {
            case 1:
                devices = ShareManager.getInstance().getFamillyDevices();
                break;
            case 2:
                devices = ShareManager.getInstance().getFriendDevices();
                break;
            default:
                break;
        }
        return devices;
    }

    // public HashMap<String, Scene> getScenesMap() {
    // return scenesMap;
    // }

    public void setScenesMap(HashMap<String, Scene> scenesMap) {
        this.scenesMap = scenesMap;
    }

    // public HashMap<String, Room> getRoomsMap() {
    // return roomsMap;
    // }

    public void setGroupsMap(HashMap<String, Room> roomsMap) {
        this.roomsMap = roomsMap;
    }

    public Scene getSceneById(String id) {
        return scenesMap.get(id);
    }

    public Device getDeviceById(String id) {
        // return devicesMap.get(id);
        Device device = new Device();
        Log.i("wmy", "getDeviceById id = " + id);
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (id.equals(mAllDeviceList.get(i).getDeviceID())) {
                device = mAllDeviceList.get(i);
            }
        }
        return device;
    }

    public Device getDeviceByName(String name) {
        // return devicesMap.get(id);
        Device device = new Device();
        Log.i("wmy", "getDeviceById id = " + name);
        for (int i = 0; i < mAllDeviceList.size(); i++) {
            if (name.equals(mAllDeviceList.get(i).getDeviceName())) {
                device = mAllDeviceList.get(i);
            }
        }
        return device;
    }

    public Room getRoomById(String id) {
        return roomsMap.get(id);
    }

    public void setSceneChildDevices(String id, List<Device> childDevices) {
        sceneChildDevicesMap.put(id, childDevices);
    }

    public List<Device> getSceneChildDevicesById(String id) {
        List<Device> devices = new ArrayList<Device>();
        if (sceneChildDevicesMap.get(id) != null) {
            for (int i = 0; i < sceneChildDevicesMap.get(id).size(); i++) {
                devices.add(sceneChildDevicesMap.get(id).get(i));
            }
        }
        return devices;
    }

    public List<Device> getSceneChildDevicesById1(String id) {
        List<Device> devices = new ArrayList<Device>();
        for (int i = 0; i < mScenes.size(); i++) {
            if (id.endsWith(mScenes.get(i).getSceneId())) {
                devices = mScenes.get(i).getDevices();
            }
        }
        return devices;
    }

    public void setGroupChildDevices(String id, List<Device> childDevices) {
        groupChildDevicesMap.put(id, childDevices);
    }

    public List<Device> getGroupChildDevicesById(String id) {
        List<Device> devices = new ArrayList<Device>();
        if (groupChildDevicesMap.get(id) != null) {
            for (int i = 0; i < groupChildDevicesMap.get(id).size(); i++) {
                devices.add(groupChildDevicesMap.get(id).get(i));
            }
        }
        return devices;
    }

    public List<Device> getGroupChildDevicesById1(String id) {
        List<Device> devices = new ArrayList<Device>();
        for (Room room : mRooms) {
            if (id.endsWith(room.getRoomId())) {
                for (int i = 0; i < room.getDevices().size(); i++) {
                    devices.add(room.getDevices().get(i));
                }
            }
        }
        return devices;
    }

}
